<?php

class Game extends AppModel {

    var $alias = 'Game';
    var $name = 'Game';
    var $useTable = 'games';
    var $belongsTo = array(
        'Catgame' => array(
            'className' => 'Catgame',
            'foreignKey' => 'cat_id',
            'fields' => array('Catgame.id', 'Catgame.title', 'Catgame.alias')
        ),
        'game_groups' => array(
            'className' => 'game_groups',
            'foreignKey' => 'group_id',
            'fields' => array('game_groups.id', 'game_groups.title')
        )
    );

    function delete($id = null, $cascade = true) {
        App::import('Component', 'Upload');
        $this->id = $id;
        $upload = new UploadComponent();

        //delete thumbnail
        $thumbnail = $this->field('thumbnail');
        if (is_file(Configure::read('game_image_thumb_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('game_image_thumb_absolute_path') . $thumbnail);
        }
        //delete flash
        $flash_path = $this->field('flash_path');
        if (is_file(Configure::read('game_flash_absolute_path') . $flash_path) && $flash_path != 'no_image.gif') {
            $upload->delete(Configure::read('game_flash_absolute_path') . $flash_path);
        }


        parent::delete($id, $cascade);
    }

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }
    function writeFooter($limit = 5){
        
        //for game 2D
        $html   =   '<div class="footer-column-listgame">';
        $html   .=  '<h3><a href="/game-2d.html" title="Game 2D">Game 2D</a></h3>';        
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.group_id' => 1),
                    'fields' => array('Game.id', 'Game.alias', 'Game.title'),
                    'order' => 'Game.id DESC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        $html   .=  '<ul>';
        foreach($data AS $row){
            $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
            $html       .=  '<li><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title']. '</a></li>';
        }
        $html   .=  '</ul>';
        $html   .=   '</div>';

        //for game 3D
        $html   .=   '<div class="footer-column-listgame">';
        $html   .=  '<h3><a href="/game-3d.html" title="Game 3D">Game 3D</a></h3>';
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.group_id' => 2),
                    'fields' => array('Game.id', 'Game.alias', 'Game.title'),
                    'order' => 'Game.id DESC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        $html   .=  '<ul>';
        foreach($data AS $row){
            $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
            $html       .=  '<li><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title']. '</a></li>';
        }
        $html   .=  '</ul>';
        $html   .=   '</div>';
        

        //for game nintendo
        $html   .=   '<div class="footer-column-listgame">';
        $html   .=  '<h3><a href="/games/moi-nhat.html" title="Game mới nhất">Game mới nhất</a></h3>';
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1'),
                    'fields' => array('Game.id', 'Game.alias', 'Game.title'),
                    'order' => 'Game.id DESC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        $html   .=  '<ul>';
        foreach($data AS $row){
            $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
            $html       .=  '<li><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title']. '</a></li>';
        }
        $html   .=  '</ul>';
        $html   .=   '</div>';

        //for game phổ biến
        $html   .=   '<div class="footer-column-listgame">';
        $html   .=  '<h3><a href="/games/pho-bien-nhat.html" title="Game phổ biến">Game phổ biến</a></h3>';
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.popular' => 1 ),
                    'fields' => array('Game.id', 'Game.alias', 'Game.title'),
                    'order' => 'Game.id DESC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        $html   .=  '<ul>';
        foreach($data AS $row){
            $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
            $html       .=  '<li><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title']. '</a></li>';
        }
        $html   .=  '</ul>';
        $html   .=   '</div>';

        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'footer_listgames.ctp', 'w');
        fwrite($resourse, $html);
    }
    function writeLastestGame($limit = 6){
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1'),
                    'fields' => array('Game.id', 'Game.alias', 'Game.title', 'Game.play_times'),
                    'order' => 'Game.id DESC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        $html   =   '';
        $display_none   =   '';
        $i = 1;
        foreach($data AS $row){
            if($i != 1){
                $display_none   =   ' style="display: none;"';
            }
            if($i % 3 == 1){
                $html   .=   '<ul class="list-lastest-news"' . $display_none . '>';
                $first_li   =   ' class="firstNews"';
            }else{
                $first_li   =   '';
            }
            $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
            $html       .=  '<li' . $first_li . '><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title'] . '<span> / đã chơi: ' . $row['Game']['play_times'] . '</span></a></li>';
            if($i % 3 == 0){
                $html   .=  '</ul>';
            }
            $i++;
        }
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'lastest_games.ctp', 'w');
        fwrite($resourse, $html);
    }
    function writeHomeGameBlock($cat_id, $element_name, $use_h1_tag    =   false, $limit = 8){
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.cat_id' => $cat_id),
                    'fields' => array('Game.hot_icon', 'Game.id', 'Game.alias', 'Game.title', 'Game.thumbnail', 'Game.play_times','Game.folder_url', 'Catgame.alias', 'Catgame.title', 'Catgame.id'),
                    'order' => 'Game.incat_ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));

        $html   =   '<div class="blockGame home-blockgame-' . $cat_id . '">';
        if($use_h1_tag){
            $html   .=  "<h1>{$data[0]['Catgame']['title']}</h1>";
        }else{
            $html   .=  "<strong>{$data[0]['Catgame']['title']}</strong>";
        }
        $html   .=  '<div class="blockGame-tabs">';
        $html   .=  '<ul class="block-tabs-style-1 tab-block-' . $cat_id . '">';
        $html   .=  '<li onclick="loadLastestgamebycategoryAjax(' . "{$cat_id}" . ', this);" class="active"><span class="borderleft">&nbsp;</span><a href="javascript:void(0);" title="Mới nhất">Mới nhất</a><span class="borderright"></span></li>';
        $html   .=  '<li onclick="loadHotgamebycategoryAjax(' . "{$cat_id}" . ', this);"><span class="borderleft">&nbsp;</span><a href="javascript:void(0);" title="Hay nhất">Hay nhất</a><span class="borderright"></span></li>';
        $html   .=  '<li onclick="loadPopulargamebycategoryAjax(' . "{$cat_id}" . ', this);"><span class="borderleft">&nbsp;</span><a href="javascript:void(0);" title="Nhiều người chơi nhất">Nhiều người chơi nhất</a><span class="borderright"></span></li>';
        $html   .=  '</ul>';
        $html   .=  '<p class="blockGame-view-all"> <span><img alt="xem tất cả" src="/img/arrow-small-icon.gif" width="3px" height="6px" /></span> <span><img alt="xem tất cả" src="/img/arrow-small-icon.gif" width="3px" height="6px" /></span> <a title="xem tất cả" href="/danh-muc-game/' . $data[0]['Catgame']['id'] . '-' . $data[0]['Catgame']['alias'] . '.html">Xem Tất cả</a> </p>';
        $html   .=  '</div>';
        $html   .=  '<div class="home-block-' . $cat_id . '">';
        $i = 1;
        foreach($data AS $row){
            if($i == 1|| $i == 5){
                $html   .=  '<div class="list-game-row">';
                $html   .=  '<ul class="list-game-lastest">';
            }
            $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
            $thumbnail  = Configure::read('game_flash_thumbnail_url_path'). $row['Game']['folder_url'] . $row['Game']['thumbnail'];
            $html   .=  '<li>';
            if($row['Game']['hot_icon']){            
                $html   .=  '<p class="game-thumb"><img class="hot-icon" src="/img/icon-hot.png" alt="Game hot" width="31px" height="32px" /><a href="' . $link_url . '" title="' . $row['Game']['title'] . '"><img src="' . $thumbnail . '" alt="' . $row['Game']['title'] . '" width="100px" height="80px" /></a></p>';
            }else{
                $html   .=  '<p class="game-thumb"><a href="' . $link_url . '" title="' . $row['Game']['title'] . '"><img src="' . $thumbnail . '" alt="' . $row['Game']['title'] . '" width="100px" height="80px" /></a></p>';                
            }
            $html   .=  '<p class="game-detail"> <strong><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title'] . '</a></strong> <span>Đã chơi: ' . $row['Game']['play_times'] . '</span> </p>';
            $html   .=  '</li>';
            
            if($i == 8 || $i == 4){
                $html   .=  '</ul>';
                $html   .=  '</div>';
            }
            $i++;

        }
        $html   .=  '<p class="blockGame-viewmore"> <span><img src="/img/arrow-small-icon.gif" width="3px" height="6px" alt="xem thêm" /></span> <span><img src="/img/arrow-small-icon.gif" width="3px" height="6px" alt="Xem thêm" /></span> <a href="/danh-muc-game/moi-nhat/' . $data[0]['Catgame']['id'] . '-' . $data[0]['Catgame']['alias'] . '.html" title="Xem Thêm">Xem Thêm</a> </p>';
        $html   .=  '</div>';
     
        $html   .=  '</div>';
            $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . $element_name . '.ctp', 'w');
            fwrite($resourse, $html);
            
        //set cache gamehot
        $cacheName  =   'lastestgame-cat-' . $cat_id;
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.cat_id' => $cat_id),
                    'fields' => array('Game.id', 'Game.alias','Game.hot_icon', 'Game.title', 'Game.thumbnail', 'Game.play_times','Game.folder_url', 'Catgame.alias', 'Catgame.title', 'Catgame.id'),
                    'order' => 'Game.incat_ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        Cache::write($cacheName, $data, 'games');
        
        //set cache gamehot
        $cacheName  =   'gamehot-cat-' . $cat_id;
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.cat_id' => $cat_id, 'Game.hot_game_incat' => '1'),
                    'fields' => array('Game.id', 'Game.alias','Game.hot_icon', 'Game.title', 'Game.thumbnail', 'Game.play_times','Game.folder_url', 'Catgame.alias', 'Catgame.title', 'Catgame.id'),
                    'order' => 'Game.hot_game_incat_ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        Cache::write($cacheName, $data, 'games');
        
        //set cache game popular
        $cacheName  =   'gamepopular-cat-' . $cat_id;
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.cat_id' => $cat_id, 'Game.popular_incat' => '1'),
                    'fields' => array('Game.id', 'Game.alias', 'Game.hot_icon', 'Game.title', 'Game.thumbnail', 'Game.play_times','Game.folder_url', 'Catgame.alias', 'Catgame.title', 'Catgame.id'),
                    'order' => 'Game.popular_incat_ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  0
        ));
        Cache::write($cacheName, $data, 'games');
    }
    function writeGamehotdaily($limit = 10){
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.hot_game_daily' => '1'),
                    'fields' => array('Game.id', 'Game.alias', 'Game.hot_icon', 'Game.title', 'Game.thumbnail', 'Game.folder_url'),
                    'order' => 'Game.hot_game_daily_ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  0
                ));
        $html   =   '';
        if(sizeof($data) >= 5){
            for($i = 0; $i < $limit; $i++){
                $row    =   $data[$i];
                $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
                $thumbnail  = Configure::read('game_flash_thumbnail_url_path'). $row['Game']['folder_url'] . $row['Game']['thumbnail'];
                $html   .=  '<div class="dailygame-row"><h3><img src="' . $thumbnail . '" height="34px" width="34px" alt="' . $row['Game']['title'] . '" /></h3>';
                $html   .=  '<p><span><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title'] . '</a></span></p>';
                $html   .=  '</div>';
            }
            $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'hotgamedailyblock.ctp', 'w');
            fwrite($resourse, $html);
        }
        
    }
    function writeGamehotdisplay($limit = 5){
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.hot_game' => '1'),
                    'fields' => array('Game.id', 'Game.alias', 'Game.title', 'Game.thumbnail', 'Game.folder_url'),
                    'order' => 'Game.hot_game_ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  0
                ));
        $html   =   '';
        $class_row  =   '';
        if(sizeof($data) >= $limit){
            $html   .=  '<div class="list-game">';
            for($i = 0; $i < $limit; $i++){
                $row    =   $data[$i];
                $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
                $thumbnail  = Configure::read('game_flash_thumbnail_url_path'). $row['Game']['folder_url'] . $row['Game']['thumbnail'];
                if($i != $limit - 1){
                    $class_row  =   'row-game';
                }else{
                    $class_row  =   'row-game last';
                }
                $html   .=  '<div class="' . $class_row . '"><h3><img src="' . $thumbnail . '" height="34px" width="34px" alt="' . $row['Game']['title'] . '" /></h3>';
                $html   .=  '<p><span><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title'] . '</a></span></p>';
                $html   .=  '</div>';
            }
            $html   .=   '</div>';
            $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'hotgameblock_display.ctp', 'w');
            fwrite($resourse, $html);
        }

    }
    function writeGamehotdisplaynone($limit = 5){
        $data = $this->find('all', array(
                    'conditions' => array('Game.published' => '1', 'Game.hot_game' => '1'),
                    'fields' => array('Game.id', 'Game.alias', 'Game.title', 'Game.thumbnail', 'Game.folder_url'),
                    'order' => 'Game.hot_game_ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  $limit
                ));
        $html   =   '';
        $class_row  =   '';
        if(sizeof($data) >= $limit){
            $html   .=  '<div class="list-game" style="display: none;">';
            for($i = 0; $i < $limit; $i++){
                $row    =   $data[$i];
                $link_url   =   "/choi-game/{$row['Game']['id']}-{$row['Game']['alias']}.html";
                $thumbnail  = Configure::read('game_flash_thumbnail_url_path'). $row['Game']['folder_url'] . $row['Game']['thumbnail'];
                if($i != $limit - 1){
                    $class_row  =   'row-game';
                }else{
                    $class_row  =   'row-game last';
                }
                $html   .=  '<div class="' . $class_row . '"><h3><img src="' . $thumbnail . '" height="34px" width="34px" alt="' . $row['Game']['title'] . '" /></h3>';
                $html   .=  '<p><span><a href="' . $link_url . '" title="' . $row['Game']['title'] . '">' . $row['Game']['title'] . '</a></span></p>';
                $html   .=  '</div>';
            }
            $html   .=   '</div>';
            $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'hotgameblock_displaynone.ctp', 'w');
            fwrite($resourse, $html);
        }

    }
    function get_all(){
        $data = $this->find('all', array(
                    'conditions' => array('Game.content_type' => 'swf'),
                    'fields' => array('Game.id', 'Game.filename', 'Game.folder_path', 'Game.flash_version', 'Game.tags'),
                ));
        return $data;
    }
}

?>
